package com.moon.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moon.entity.User;
import com.moon.service.UserService;
import com.moon.utils.BaseController;

@Controller
@RequestMapping("/moon/User")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;

	@RequestMapping("/login")
	public String login(String account, String password, HttpSession sess) {
		User user = userService.selectByAccountAndPassword(account, password);
		if (null == user) {
			return "redirect:/login";
		}
		// success
		sess.setAttribute("user", user);
		return "redirect:/";
	}
	//使用jsonp方式跨域传参+前端发请求给本域服务器,本域服务器转发请求给外域服务器+node.js服务器转发或nginx服务器转发
	@RequestMapping("/test")
	public void test(String callback,HttpServletResponse res) throws IOException {
		String s = "success";
		res.getWriter().write(callback+"('"+s+"')");;
		
	}
	@RequestMapping("/logout")
	public String logout(HttpSession sess) {
		sess.removeAttribute("user");
		return "redirect:/";
	}

}